package com.example.saveimageandvideo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.webkit.DownloadListener;
import android.widget.TextView;
import android.widget.Toast;

import com.example.saveimageandvideo.manager.AndroidDownloadManager;
import com.example.saveimageandvideo.manager.AndroidDownloadManagerListener;
import com.example.saveimageandvideo.util.FileUtils;
import com.example.saveimageandvideo.util.PermissionsUtil;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private TextView tvSaveImage, tvSaveVideo;
    private boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 请求权限
        loadPermission();
        initView();
        initListener();
    }

    /**
     * 动态请求权限
     */
    private void loadPermission() {
        PermissionsUtil.requestCamera(this, aBoolean -> {

                }
        );
    }

    private void initView() {
        tvSaveImage = findViewById(R.id.tv_save_image);
        tvSaveVideo = findViewById(R.id.tv_save_video);
    }

    private void initListener() {
        tvSaveImage.setOnClickListener(v -> {
            if (false) {
                Toast.makeText(MainActivity.this, "图片开始下载", Toast.LENGTH_SHORT).show();
                flag = false;
            } else {
                Toast.makeText(MainActivity.this, "图片正在下载", Toast.LENGTH_SHORT).show();
            }
            //String url = "https://pic.cnblogs.com/avatar/1142647/20170416093225.png";
            //String url = "http://ww3.sinaimg.cn/bmiddle/6e91531djw1e8l3c7wo7xj20f00qo755.jpg";
            //String url = "https://ae01.alicdn.com/kf/U29ad1424fd024374bf8ba95a61a60d8ai.jpg";
            String url = "https://ae01.alicdn.com/kf/Ua227945b506241af975a9b0a16d6df3bA.jpg";
            //保存图片
            downLoadImage(url);
        });

        tvSaveVideo.setOnClickListener(v -> {
            if (false) {
                Toast.makeText(MainActivity.this, "视频下载开始", Toast.LENGTH_SHORT).show();
                flag = false;
            } else {
                Toast.makeText(MainActivity.this, "视频正在下载", Toast.LENGTH_SHORT).show();
            }
            downLoadVideo("https://www.w3school.com.cn/example/html5/mov_bbb.mp4");
        });
    }

    /**
     * 保存图片到相册
     * @param path
     */
    private void downLoadImage(String path) {
        new Thread(() -> new AndroidDownloadManager(MainActivity.this, path)
                .setListener(new AndroidDownloadManagerListener() {
                    @Override
                    public void onPrepare() {
                        Log.d("downloadVideo", "onPrepare");
                    }

                    @Override
                    public void onSuccess(String path) {
                        Toast.makeText(MainActivity.this, "图片已保存到相册", Toast.LENGTH_SHORT).show();
                        FileUtils.saveImage(MainActivity.this,new File(path));
                        flag = true;
                        Log.d("downloadVideo", "onSuccess >>>>" + path);
                    }

                    @Override
                    public void onFailed(Throwable throwable) {
                        Toast.makeText(MainActivity.this, "图片下载失败，请重新下载！", Toast.LENGTH_SHORT).show();
                        Log.e("downloadVideo", "onFailed", throwable);
                        flag = true;
                    }
                }).download()).start();
    }

    /**
     * 保存视频到相册
     * @param path
     */
    private void downLoadVideo(String path) {
        new Thread(() -> new AndroidDownloadManager(MainActivity.this, path)
                .setListener(new AndroidDownloadManagerListener() {
                    @Override
                    public void onPrepare() {
                        Log.d("downloadVideo", "onPrepare");
                    }

                    @Override
                    public void onSuccess(String path) {
                        Toast.makeText(MainActivity.this, "视频已保存到相册", Toast.LENGTH_SHORT).show();
                        FileUtils.saveVideo(MainActivity.this,new File(path));
                        flag = true;
                        Log.d("downloadVideo", "onSuccess >>>>" + path);
                    }

                    @Override
                    public void onFailed(Throwable throwable) {
                        Toast.makeText(MainActivity.this, "视频下载失败，请重新下载！", Toast.LENGTH_SHORT).show();
                        Log.e("downloadVideo", "onFailed", throwable);
                        flag = true;
                    }
                }).download()).start();
    }


}
